"use strict";

const fs = require("node:fs");
const path = require("node:path");

// 递归遍历文件夹下所有文件
/**
 * @param {string} dir
 * @param {string[]} fileList
 * @returns {string[]}
 */
function findCSVFilesPath(dir, fileList = []) {
  const files = fs.readdirSync(dir);

  for (const file of files) {
    const filePath = path.join(dir, file);
    const stat = fs.statSync(filePath);

    if (stat.isDirectory()) {
      // 如果是目录，递归遍历
      findCSVFilesPath(filePath, fileList);
      // 过滤出所有CSV文件
    } else if (path.extname(file).toLowerCase() === ".csv") {
      // 如果是 .csv 文件，添加到数组
      fileList.push(filePath);
    }
  }

  return fileList;
}

module.exports = {
  findCSVFilesPath,
};
